package com.surenpi.jenkins.client.util;

import com.surenpi.jenkins.client.folder.FolderJob;
import java.net.URI;

public final class UrlUtils {
   private static final int DEFAULT_BUFFER_SIZE = 64;

   private UrlUtils() {
   }

   public static String toBaseUrl(final FolderJob folder) {
      return folder == null ? "/" : folder.getUrl();
   }

   public static String toJobBaseUrl(final FolderJob folder, final String jobName) {
      StringBuilder sb = new StringBuilder(64);
      sb.append(toBaseUrl(folder));
      if (sb.charAt(sb.length() - 1) != '/') {
         sb.append('/');
      }

      sb.append("job/");
      String[] jobNameParts = jobName.split("/");

      for(int i = 0; i < jobNameParts.length; ++i) {
         sb.append(EncodingUtils.encode(jobNameParts[i]));
         if (i != jobNameParts.length - 1) {
            sb.append('/');
         }
      }

      return sb.toString();
   }

   public static String toViewBaseUrl(final FolderJob folder, final String name) {
      StringBuilder sb = new StringBuilder(64);
      String base = toBaseUrl(folder);
      sb.append(base);
      if (!base.endsWith("/")) {
         sb.append('/');
      }

      sb.append("view/").append(EncodingUtils.encode(name));
      return sb.toString();
   }

   public static String toFullJobPath(final String jobName) {
      String[] parts = jobName.split("/");
      if (parts.length == 1) {
         return parts[0];
      } else {
         StringBuilder sb = new StringBuilder(64);

         for(int i = 0; i < parts.length; ++i) {
            sb.append(parts[i]);
            if (i != parts.length - 1) {
               sb.append("/job/");
            }
         }

         return sb.toString();
      }
   }

   public static String join(final String path1, final String path2) {
      if (path1.isEmpty() && path2.isEmpty()) {
         return "";
      } else if (path1.isEmpty() && !path2.isEmpty()) {
         return path2;
      } else if (path2.isEmpty() && !path1.isEmpty()) {
         return path1;
      } else {
         StringBuilder sb = new StringBuilder(64);
         sb.append(path1);
         if (sb.charAt(sb.length() - 1) == '/') {
            sb.setLength(sb.length() - 1);
         }

         if (path2.charAt(0) != '/') {
            sb.append('/');
         }

         sb.append(path2);
         return sb.toString();
      }
   }

   public static URI toJsonApiUri(final URI uri, final String context, final String path) {
      String p = path;
      if (!path.matches("(?i)https?://.*")) {
         p = join(context, path);
      }

      if (p.startsWith("/blue/rest/organizations/jenkins/pipelines")) {
         return uri.resolve("/").resolve(p.replace(" ", "%20"));
      } else {
         if (!p.contains("?")) {
            p = join(p, "api/json");
         } else {
            String[] components = p.split("\\?", 2);
            p = join(components[0], "api/json") + "?" + components[1];
         }

         return uri.resolve("/").resolve(p.replace(" ", "%20"));
      }
   }

   public static URI toNoApiUri(final URI uri, final String context, final String path) {
      String p = path.matches("(?i)https?://.*") ? path : join(context, path);
      return uri.resolve("/").resolve(p);
   }
}
